#region Imports

using System.Collections.Generic;
using System.Globalization;
using IEC.ColumnOrdering.SP2007.Application.Domain;
using Microsoft.SharePoint;

#endregion

namespace IEC.ColumnOrdering.SP2007.Application.Services.Concrete
{

    /// <summary>
    /// Provides functionality to convert fields.
    /// </summary>
    internal static class FieldConverter
    {

        #region Constants

        /// <summary>
        /// Represents the string format of the authoring display name.
        /// </summary>
        private const string AuthoringDisplayNameStringFormat = @"{0} {1}";

        #endregion

        #region Methods - Static Member

        /// <summary>
        /// Converts the given SharePoint field collection to a collection of fields.
        /// </summary>
        /// <param name="sharepointViewFieldCollection">
        /// The SharePoint view field collection to be converted.
        /// </param>
        /// <param name="sharepointListFieldCollection">
        /// The SharePoint list field collection information, to retrieve detailed information.
        /// </param>
        /// <returns>
        /// The result of converting <paramref name="sharepointViewFieldCollection"/>.
        /// </returns>
        public static FieldCollection Convert(
            SPViewFieldCollection sharepointViewFieldCollection, 
            SPFieldCollection sharepointListFieldCollection)
        {
            FieldCollection fieldCollection = new FieldCollection();

            IDictionary<string, SPField> listFieldIndex = new Dictionary<string, SPField>();

            foreach (SPField sharepointListField in sharepointListFieldCollection)
            {
                listFieldIndex[sharepointListField.InternalName] = sharepointListField;
            }

            foreach (string sharepointViewField in sharepointViewFieldCollection)
            {
                if (listFieldIndex.ContainsKey(sharepointViewField))
                {
                    SPField sharepointListField = listFieldIndex[sharepointViewField];

                    Field field = FieldConverter.Convert(sharepointListField);

                    fieldCollection.Add(field);
                }
            }

            return fieldCollection;
        }

        /// <summary>
        /// Converts the given SharePoint field collection to a collection of fields.
        /// </summary>
        /// <param name="sharepointFieldCollection">
        /// The SharePoint field collection to be converted.
        /// </param>
        /// <returns>
        /// The result of converting <paramref name="sharepointFieldCollection"/>.
        /// </returns>
        public static FieldCollection Convert(
            SPFieldCollection sharepointFieldCollection)
        {
            FieldCollection fieldCollection = new FieldCollection();

            foreach (SPField sharepointViewField in sharepointFieldCollection)
            {
                Field field = FieldConverter.Convert(sharepointViewField);

                fieldCollection.Add(field);
            }

            return fieldCollection;
        }

        /// <summary>
        /// Converts a given SharePoint field to a field.
        /// </summary>
        /// <param name="sharepointViewField">
        /// The SharePoint view to be converted.
        /// </param>
        /// <returns>
        /// The result of converting <paramref name="sharepointViewField"/>.
        /// </returns>
        public static Field Convert(SPField sharepointViewField)
        {
            Field field = new Field();

            // set properties
            field.Id = sharepointViewField.Id;
            field.Name = sharepointViewField.InternalName;
            field.DisplayName = sharepointViewField.Title;
            field.HasAuthoringInfo = (!(string.IsNullOrEmpty(sharepointViewField.AuthoringInfo)));
            field.AuthoringDisplayName = string.Format(
                CultureInfo.CurrentUICulture,
                FieldConverter.AuthoringDisplayNameStringFormat,
                sharepointViewField.Title,
                sharepointViewField.AuthoringInfo);
            field.Description = sharepointViewField.Description;
            field.DataSpecification = string.Empty;
            field.IsHidden = sharepointViewField.Hidden;
            
            return field;
        }

        #endregion

    }

}
